import { onMounted, ref } from 'vue';
import { useIntersectionObserver, useIntervalFn } from '@vueuse/core';

export function useLazyData(fn: () => void) {
  const target = ref(null)
// 在可视区域后在发请求
onMounted(() => {
  const { stop } = useIntersectionObserver(target.value, ([{ isIntersecting }]) => {
    if (isIntersecting) {
      // 发送请求
      fn()
      // 停止监视
      stop()
    }
  })
})
return target
}
// 验证码的倒计时
export   function useCountDown() {
  const time = ref(0)
  const { pause, resume } = useIntervalFn(() => {
  if (time.value === 0) {
    pause()
  } else {
    time.value--
  }
  }, 1000, {immediate: false})
  const start = (num: number) => {
    // 开启倒计时
      time.value = num
      resume()
  }
  return {time, start}
}